const express = require('express');
const router = express.Router();
const Blog = require('./../../mongoModuels/stateg/blog');
const time  = require('./../../utils/time');
const random = require('./../../utils/random');


router
.get('/list', (req, res) => {
    let pageNum = parseInt(req.query.pageNum);
    let pageSize = parseInt(req.query.pageSize);
    let title = new RegExp(req.query.name, 'i');
    // 获取传参
    let query;
    let isType
    if(req.query.type) {
        isType = { '$or': [{ 'title': title }], 'type': 1, 'mold': parseInt(req.query.type) };
    } else {
        isType = { '$or': [{ 'title': title }], 'type': 1, };
    }
    query = Blog.blogList.find(isType).sort({'creationTime': -1});
    // 统计数据
    Blog.blogList.count(isType,  (err, count) => {
        query.skip((pageNum - 1) * pageSize).limit(pageSize);
        // 分页
        query.exec({}, (err, data) => {
            if (err) {
                res.json({
                    status: 0,
                    message: err
                })
            } else if (!pageNum || !pageSize) {
                res.json({
                    status: 0,
                    message: '参数不全'
                })
            } else {
				data.forEach(item => {
					if(!item.browse) {
						item.browse = 0;
					}
				})
                res.json({
                    status: 1,
                    message: '操作成功',
                    data: {
                        data,
                        count
                    }
                })
            }
        })
    })
})
//查看列表

.post('/list/msg', (req, res) => {
    let creationTime = time(new Date().getTime());
    if(req.body.id && req.body.name && req.body.info) {
        data = {
            name: req.body.name,
            mailbox: req.body.mailbox,
            info: req.body.info,
            mag: [],
            type: 1,
            creationTime: creationTime,
        }
        if(req.body.curId) {
            let id = random();
            data._id = id;
            Blog.articleMas.updateOne({'_id': req.body.curId}, {$push: {'mag': data}}).then(() => {
                res.json({
                    status: 1,
                    message: '评论成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '评论失败'
                })
            })
        } else {
            Blog.articleMas.create(data, (err, rows) => {
                if(err) {
                    res.json({
                        status: 0,
                        message: '评论失败'
                    })
                } else {
                    Blog.blogList.updateOne({'_id': req.body.id}, {$push: {'mag': rows._id}}).then(() => {
                        res.json({
                            status: 1,
                            message: '评论成功'
                        })
                    }).catch(() => {
                        res.json({
                            status: 0,
                            message: '评论失败'
                        })
                    })
                }
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不全'
        })
    }
})
//留言

.get('/article/details', (req, res) => {
    if(req.query.id) {
        let id = req.query.id;
        let query = Blog.blogList.findOne({'_id': id, 'type': 1});
        query.select('-mailbox');
        query.exec({}, (err, data) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            } else {
                getMas(data, res, req.query.id)
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不全'
        })
    }
})

const getMas = (data, res, id) => {
    let query = Blog.articleMas.find({'_id': {$in: data.mag}}).sort({'creationTime': -1});
    query.exec({}).then(rows => {
        rows.forEach((item, i, e) => {
            if(item.type === 2) {
                e.splice(i, 1);
            }
            if(item.mag.length > 0) {
                item.mag.forEach((items, index, event) => {
                    if(items.type === 2) {
                        event.splice(index, 1);
                    }
                })
            }
        })
        browse(data, id);
        res.json({
            status: 1,
            message: '操作成功',
            data,
            rows
        })
    }).catch(() => {
        res.json({
            status: 1,
            message: '操作失败'
        })
    })
}

const browse = (data, id) => {
	if(data.browse) {
		data.browse+=1;
	} else {
		data.browse = 1;
	}
    Blog.blogList.updateOne({'_id': id}, data).then(res => {
        
    })
}
//详情

module.exports = router;